<?php

/**
 * componentsbyclient actions.
 *
 * @package    student_manager
 * @subpackage componentsbyclient
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class componentsbyclientActions extends autocomponentsbyclientActions
{
 public function executeList()
  {
    $this->getResponse()->setTitle('WMS-Component-By-Client-List');
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/components_by_client/filters');

    // pager
    $this->pager = new sfPropelPager('ComponentsByClient', 25);
    $c = new Criteria();
    $c->addDescendingOrderByColumn(ComponentsByClientPeer::ID);
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/components_by_client')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/components_by_client');
    }
  }
  public function executeEdit()
  {
     $this->getResponse()->setTitle('WMS-Edit-Component-By-Client');
    $this->components_by_client = $this->getComponentsByClientOrCreate();
    
    if ($this->getRequestParameter('id')=='' ||  $this->getRequestParameter('id')==null){
        $this->getRequest()->setError('edit', 'Cannot create ComponentsByClient.');        
        $this->forward('componentsbyclient', 'list');
    }
    if( $this->components_by_client->getStatus()== CommonValues::$gCBCStatus['key']['Revised'] ) {
        $this->getRequest()->setError('edit', 'Cannot edit this ComponentsByClient. It was revised.');        
        $this->forward('componentsbyclient', 'list');
    }    
    if( $this->components_by_client->getStatus()== CommonValues::$gCBCStatus['key']['Completed'] ) {
        $this->getRequest()->setError('edit', 'Cannot edit this ComponentsByClient. It was Completed.');        
        $this->forward('componentsbyclient', 'list');
    }

    if ($this->getRequest()->isMethod('post'))
    {
      $this->updateComponentsByClientFromRequest();
      
      $rowIds = $this->getRequestParameter('rowIds');
      
      $qty2s = $this->getRequestParameter('qty2s');
      $qty3s = $this->getRequestParameter('qty3s');
      $qty4s = $this->getRequestParameter('qty4s');
      
      if ($rowIds && count($rowIds)){
      
        $componentgroupId = $this->components_by_client->getComponentgroupId();        
        
        for ($i =0; $i < count($rowIds); $i++){
            $c = new Criteria();
            $c->add(ComponentsPeer::COMPONENT_ID , $rowIds[$i]);
            $c->add(ComponentsPeer::COMPONENTGROUP_ID , $componentgroupId);
            $components = ComponentsPeer::doSelectOne($c);
            
            $components->setQty2($qty2s[$i]);
            $components->setQty3($qty3s[$i]);
            $components->setQty4($qty4s[$i]);
            
            $components->save();
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID , $componentgroupId);
        $compsList= ComponentsPeer::doSelect($c);
        
        $bComplete =true;
        foreach ($compsList as $comps){
            if ($comps->getQty1() != $comps->getQty2() || ($comps->getQty2() != $comps->getQty3() + $comps->getQty4())){
                $bComplete = false;
            }
        }
        if ($bComplete) {
            $this->components_by_client->setStatus(CommonValues::$gCBCStatus['key']['Completed']);;
        }
        
      }

      try
      {
        $this->saveComponentsByClient($this->components_by_client);
      }
      catch (PropelException $e)
      {
        $this->getRequest()->setError('edit', 'Could not save the edited Components by clients.');
        return $this->forward('componentsbyclient', 'list');
      }

      $this->getUser()->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('componentsbyclient/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('componentsbyclient/list');
      }
      else
      {
        return $this->redirect('componentsbyclient/edit?id='.$this->components_by_client->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }
  
  
  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->components_by_client = $this->getComponentsByClientOrCreate();
    $this->updateComponentsByClientFromRequest();

    $this->labels = $this->getLabels();
    
    //transfer data to edit_form template
    $this->components_by_client->qtys = $this->getRequestParameter('qtys');
    $this->components_by_client->qty2s = $this->getRequestParameter('qty2s');
    $this->components_by_client->qty3s = $this->getRequestParameter('qty3s');
    $this->components_by_client->qty4s = $this->getRequestParameter('qty4s');
    $this->components_by_client->rowIds = $this->getRequestParameter('rowIds');

    return sfView::SUCCESS;
  }
  
    protected function getLabels()
  {
    return array(
      'components_by_client{id}' => 'Id:',
      'components_by_client{bom_id}' => 'Bom:',
      'components_by_client{project_id}' => 'Project:',
      'components_by_client{client_id}' => 'Client:',
      'components_by_client{componentgroup_id}' => 'Component List:',
      'components_by_client{created_at}' => 'Created at:',
      'qty2s' => 'Component List:',
      'qty3s' => 'Component List:',
      'qty4s' => 'Component List:',
      'wrong' => 'Component List:',
      
    );
  }
  
  
  public function executeDelete()
  {
   
        $this->getRequest()->setError('delete', 'Cannot delete ComponentsByClient.');        
        $this->forward('componentsbyclient', 'list');
  
  }

}
